<script type="text/javascript">
  RED.nodes.registerType('ads-client-read-system-manager-state', {
    paletteLabel: 'ADS - Read System Manager State',
    category: 'TwinCAT ADS',
    icon: 'font-awesome/fa-refresh',
    color: '#3FADB5',
    inputs: 1,
    inputLabels: function() {
      return "Read trigger"
    },
    outputs: 1,
    outputLabels: 'Data',
    defaults: { 
      name: { value: '' },
      connection: { value: null, type: "ads-client-connection" }
    },
    label: function () {
      if(this.connection === null)
        return  `${(this.name || `ADS - Read System Manager State`)} (*Not configured*)`;
 
      if (this.name) {
        return this.name
      }

      return `ADS - Read System Manager State`;
    },
    oneditprepare: function () {
      //Stuff that is done when properties panel is opened
    }
  })
</script>



<!-- Properties -->
<script type="text/html" data-template-name="ads-client-read-system-manager-state">
  <div class="form-row">
      <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
      <input type="text" id="node-input-name" placeholder="Name (optional)">
  </div>

  <div class="form-row">
      <label for="node-input-connection"><i class="fa fa-bookmark"></i> ADS connection</label>
      <input type="text" id="node-input-connection" placeholder="ADS Connection">
  </div>

  <div class="form-row">
    <!-- Some margin after tip -->
  </div>
</script>



<!-- Help -->
<script type="text/html" data-help-name="ads-client-read-system-manager-state">
<p>
  Reads TwinCAT System Manager state from target (using ADS port 10000).
</p>
<p>
  <b>NOTE:</b> Used <code>ads-connection</code> needs to have <code>allowHalfOpen</code> setting checked in order to connect to systems running in Config mode.
</p>


<h3>Properties (settings)</h3>
  <dl class="message-properties">
    <dt>Name
      <span class="property-type">string</span>
    </dt>
    <dd> Optional node name</dd>
  </dl>
  
  <dl class="message-properties">
    <dt>ADS connection
      <span class="property-type">ads-client-connection</span>
    </dt>
    <dd> ADS client connection (target system) to use</dd>
  </dl>


<h3>Outputs</h3>
  <dl class="message-properties">
    <dt>payload <span class="property-type">string</span></dt>
    <dd>The system manager state as string (Run, Config.. - See <a href="https://github.com/jisotalo/ads-client/blob/f1a5a07bd0c847b8a92a512c8b99b78eb7838b88/src/ads-client-ads.js#L469" target="_blank">ADS_STATE</a>)</dd>
    <dt>result <span class="property-type">object</span></dt>
    <dd>The system manager state object</dd>
    <dt>... <span class="property-type">any</span></dt>
    <dd>All input <code>msg</code> properties are forwarded</dd>
  </dl>

<h3>Details</h3>
  <p>
    Reads the target system manager state (Run, Config).
  </p>
  <p>
   <b>NOTE:</b> Used <code>ads-connection</code> needs to have <code>allowHalfOpen</code> setting checked in order to connect to systems running in Config mode.
  </p>
  <p>
    See <code>ads-client</code> readme for help.
  </p>


<h3>References</h3>
  <ul>
    <li><a href="https://github.com/jisotalo/ads-client#starting-and-stopping-the-twincat-system" target="_blank" style="text-decoration: underline">
      ads-client readme
    </a></li>
    <li><a href="https://jisotalo.github.io/ads-client/Client.html#readSystemManagerState" target="_blank" style="text-decoration: underline">
      ads-client readSystemManagerState() documentation
    </a></li>
    <li><a href="https://github.com/jisotalo/ads-client/blob/f1a5a07bd0c847b8a92a512c8b99b78eb7838b88/src/ads-client-ads.js#L469" target="_blank" style="text-decoration: underline">
      Possible states (ADS_STATE)
    </a></li>
  </ul>
</script>